import os
import urllib

from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp                                        
import os
from google.appengine.api import users
from utilities import *
from entities import * 

from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp.util import login_required
from xmlWs import XmlUtilities 

class AddApp(webapp.RequestHandler):
    @login_required
    def get(self):
        upload_url = blobstore.create_upload_url('/AddApp')
        #Device.Add("Phone")
        template_values = {
            'header': SiteMaster.Header(self,"topnav_addApp"),
            'footer': SiteMaster.Footer(),
            'css'   : SiteMaster.CSS(),
            'languages': Language.Get(),
            'categorys': Category.Get(),
            'versions' : Version.Get(),
            'devices'  : Device.Get(),
            'url'      : upload_url,
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/AddApp.html')
        self.response.out.write(template.render(path, template_values))
        
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):        
    def post(self):
        
        appName = self.request.get('AppName')
        title = self.request.get('Title')
        categoryID = int(self.request.get('ddlCategory'))
        deviceID = int(self.request.get('ddlDevice'))
        keyWord = self.request.get('Keyword')
        description = self.request.get('Description')
        languageID = int(self.request.get('ddlLanguage'))
        versionID = int(self.request.get('ddlMinversion'))
        price = float(self.request.get('Price'))
        releaseDate = self.request.get('ReleaseDate')
        upload_files1 = self.get_uploads('imgIcon')
        appIcon = upload_files1[0]
        upload_files2 = self.get_uploads('imgScreenshots')
        appScreenshots = upload_files2[0]
        upload_files = self.get_uploads('file')
        appSource = upload_files[0]
        user = users.get_current_user()
        devID = Developer.GetID(user)
        status = App.Add(title,appName,description,price,keyWord,languageID,categoryID,deviceID,devID,releaseDate,versionID,appIcon,appScreenshots,0,appSource)
        template_values = {
            'status':status,
            'header': SiteMaster.Header(self,"topnav_home"),
            'footer': SiteMaster.Footer(),
            'css'   : SiteMaster.CSS(),
           
        }
        self.redirect('/MyApp')
        #path = os.path.join(os.path.dirname(__file__), '../templates/RegisterResult.html')
        #self.response.out.write(template.render(path, template_values))

class ServeHandlerApp(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self, app_id):
        apps = Entities.App.get_by_id(int(app_id))
        blob_key = str(apps.AppSource.key())
        blob_key = str(urllib.unquote(blob_key))
        if not blobstore.get(blob_key):
            self.error(404)
        else:
            self.send_blob(blobstore.BlobInfo.get(blob_key),save_as=True)
    
class ServerHandlerIcon(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self,app_id):
        apps = Entities.App.get_by_id(int(app_id))
        blob_key = str(apps.AppIcon.key())
        blob_key = str(urllib.unquote(blob_key))
        if not blobstore.get(blob_key):
            self.error(404)
        else:
            self.send_blob(blobstore.BlobInfo.get(blob_key),save_as=False)
            
class ServerHandlerScreen(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self,app_id):
        apps = Entities.App.get_by_id(int(app_id))
        blob_key = str(apps.AppScreenShot.key())
        blob_key = str(urllib.unquote(blob_key))
        if not blobstore.get(blob_key):
            self.error(404)
        else:
            self.send_blob(blobstore.BlobInfo.get(blob_key),save_as=False)
        
class MyApp(webapp.RequestHandler):
   
    def get(self):
        user = users.get_current_user()
        devID = Developer.GetID(user)
        app = App.GetbyDev(devID)
        #testXml = XmlUtilities.Application()
        #test = App.GetbyCategory("http://localhost:8087","Game")
        #test = testXml.GetHotApp("http://localhost:8087")
        template_values = {
            'header': SiteMaster.Header(self,"topnav_MyApp"),
            'footer': SiteMaster.Footer(),
            'css'   : SiteMaster.CSS(),
            'apps'  : app,
         
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/MyApp.html')
        self.response.out.write(template.render(path, template_values))
        
    